#include "stdafx.h"
#include "DeviceCaps.h"
#include "Log.h"
#include "Exception.h"
#include "ErrorHandlers.h"
#include "MemUtils.h"

namespace Cvekas {

const D3DCAPS9& DeviceCaps::getD3DCaps()
{
	return caps;
}

D3DDEVTYPE DeviceCaps::getDeviceType()
{
	return device_type;
}

DeviceCaps::PShaderVersion DeviceCaps::getPSVersion()
{
	return ps_version;
}

DeviceCaps::VShaderVersion DeviceCaps::getVSVersion()
{
	return vs_version;
}

uint DeviceCaps::getMRTCount()
{
	return mrt_count;
}

bool DeviceCaps::supportFloatRT()
{
	return fp_rt_support;
}

DeviceCaps::DeviceCaps(D3DPtr d3d, D3DDEVTYPE device_type)
: d3d(d3d), device_type(device_type)
{
	// TODO: output most imporant caps info to log

	E_E(d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, device_type, &caps));

	// Pixel shader
	if(caps.PixelShaderVersion >= D3DPS_VERSION(3, 0)) ps_version = ps3_0;
	else if(caps.PixelShaderVersion >= D3DPS_VERSION(2, 0)) ps_version = ps2_0;
	else if(caps.PixelShaderVersion >= D3DPS_VERSION(1, 4)) ps_version = ps1_4;
	else if(caps.PixelShaderVersion >= D3DPS_VERSION(1, 1)) ps_version = ps1_1;
	else ps_version = no_ps;

	// Vertex shader
	if(caps.VertexShaderVersion >= D3DVS_VERSION(3, 0)) vs_version = vs3_0;
	else if(caps.VertexShaderVersion >= D3DVS_VERSION(2, 0)) vs_version = vs2_0;
	else if(caps.VertexShaderVersion >= D3DVS_VERSION(1, 1)) vs_version = vs1_1;
	else vs_version = no_vs;

	// MRT
	mrt_count = caps.NumSimultaneousRTs;

	// FP rendertargets
	bool r32f = E_B(d3d->CheckDeviceFormat(D3DADAPTER_DEFAULT, device_type, 
		D3DFMT_X8R8G8B8, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_R32F));
	bool a16b16g16r16f = E_B(d3d->CheckDeviceFormat(D3DADAPTER_DEFAULT, device_type, 
		D3DFMT_X8R8G8B8, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F));

	fp_rt_support  = ps_version >= ps2_0 && r32f && a16b16g16r16f;
}

}







	



